#include <iostream>
#include <unistd.h>

typedef void (*work_t)(int);
typedef void (*task_t)(pid_t, int);

void PrintLog(pid_t id, int fd)
{
    std::cout << "pid:" << id << "fd:" << fd << "task is :" << "printf log task" << std::endl << std::endl;
}

void ReloadConf(pid_t id, int fd)
{
    std::cout << "pid:" << id << "fd:" << fd << "task is :" << "reload conf task" << std::endl << std::endl;
}

void ConnectMysql(pid_t id, int fd)
{
    std::cout << "pid:" << id << "fd:" << fd << "task is :" << "connect mysql task" << std::endl << std::endl;
}

class tasks
{
public:
    tasks()
    {
        _tasks.push_back(PrintLog);
        _tasks.push_back(ReloadConf);
        _tasks.push_back(ConnectMysql);
    }
    task_t SelectWork(uint32_t code)
    {
        return _tasks[code];
    }

protected:
    std::vector<task_t> _tasks;
};
uint32_t NextTask()
{
    return rand() % 3;
}
void worker(int fd)
{
    tasks t;
    while (true)
    {
        uint32_t command_code = 0;
        int n = read(0, &command_code, sizeof(command_code));
        if(n == 0)
        {
            std::cout << "child process close!" << std::endl;
            break;
        }
        t.SelectWork(command_code)(getpid(), fd);
        sleep(1);
    }
}